import { message, notification } from 'antd';
import {
  getCurve,
  getDates,
  addValue,
  delValue,
  getNetReportDatas,
  getNetReportDataReset,
  getNetReportDates,
} from '../../../services/third_service';
import { getDataItem } from '../../../services/newTwo_service';

export default {
  namespace: 'third',
  state: {},

  effects: {
    *GetCurve({ payload, callback }, { call, put }) {
      const response = yield call(getCurve, payload);
      if (typeof response === 'undefined') {
        return;
      }
      if (typeof response === 'undefined' || response.code === 0) {
        return;
      }
      const list = [];
      const data = response.list.list;
      for (let i = 0; i < data.length; i += 1) {
        list.push({
          date: data[i].date,
          fundNetValue: data[i].fundNetValue,
          spyNetValue: data[i].spyNetValue,
          // netBoost: parseFloat(
          //   (data[i].fundNetValue - data[i].spyNetValue).toFixed(
          //     6
          //   )
          // ),
          netBoost: data[i].netBoost,
        });
      }

      if (callback) callback({ list, name: response.list.name });
    },

    *AddValue({ payload, callback }, { call, put }) {
      const response = yield call(addValue, payload);
      if (typeof response === 'undefined') {
        return;
      }
      if (response.code === 1) {
        message.success('成功');
      } else {
        message.error(`失败:${response.message}`);
      }
      if (callback) callback(response);
    },

    *DelValue({ payload, callback }, { call, put }) {
      const response = yield call(delValue, payload);
      if (typeof response === 'undefined') {
        return;
      }
      if (response.code === 1) {
        message.success('成功');
      } else {
        message.error(`失败:${response.message}`);
      }
      if (callback) callback(response);
    },

    *GetDates({ payload, callback }, { call, put }) {
      const response = yield call(getDates, payload);
      if (typeof response === 'undefined') {
        return;
      }
      if (callback) callback(response);
    },

    *GetNetReportDates({ payload, callback }, { call, put }) {
      const response = yield call(getNetReportDates, payload);
      if (typeof response === 'undefined') {
        return;
      }
      if (callback) callback(response);
    },

    *GetNetReportDatas({ payload, callback }, { call, put }) {
      const response = yield call(getNetReportDatas, payload);
      if (typeof response === 'undefined' || response.code === 0) {
        notification.error({
          message: typeof response.message !== 'undefined' ? response.message : '请求错误',
        });
      } else if (response.code === 1) {
        if (callback) callback(response);
      }
    },

    *GetNetReportDataReset({ payload, callback }, { call, put }) {
      const response = yield call(getNetReportDataReset, payload);
      if (typeof response === 'undefined' || response.code === 0) {
        notification.error({
          message: typeof response.message !== 'undefined' ? response.message : '请求错误',
        });
      } else if (response.code === 1) {
        message.success(response.message);
        if (callback) callback(response);
      }
    },
  },

  reducers: {
    saveCharts(state, action) {
      return {
        ...state,
        charts: action.payload,
      };
    },
  },
};
